﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using DBSourceToolsLib;
using DBSourceToolsLib.Interfaces;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;
using System.Collections;
using DBSourceToolsLib.Composite;

namespace DBSourceTools
{
    public partial class DBExplorer : MainPanelForm, Interfaces.IWindowListObserver
    {
        string m_CurrentDbNickName = "";
        private TreeNode m_CurrentNode;
        private Hashtable m_ExpandedNodes = new Hashtable(); 

        public DBExplorer()
        {
            InitializeComponent();
            WindowList.Instance().RegisterInterestInWindowList(this);

            HCSocketToolsLib.HCSocketSender.Instance().SetIpAddress("127.0.0.1");
            HCSocketToolsLib.HCSocketSender.Instance().SetPort(11000);

        }

        private void DBExplorer_Load(object sender, EventArgs e)
        {
            try
            {
                if (UserSettings.Instance().DatabaseProject != null)
                {
                    for (int i = 0; i < UserSettings.Instance().DatabaseProject.SourceDatabases.Count; i++)
                    {
                        SourceDatabase oSourceDb = UserSettings.Instance().DatabaseProject.SourceDatabases[i];
                        if (i == 0)
                            this.m_CurrentDbNickName = oSourceDb.NickName;

                        DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                            new DBSourceToolsLib.SourceDatabaseLoader(this,
                                DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);

                        oSourceLoader.Execute(ref oSourceDb, null);
                        if (oSourceLoader.HasErrors())
                            throw new Exception(oSourceLoader.GetErrorMessage());
                    }
                }

                UpdateTreeViewDatabases();
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(this);

            }
        }

        public void UpdateTreeView(string strDBNickName)
        {
            

            SourceDatabase oSource = UserSettings.Instance().getSourceDatabase(strDBNickName);
            if (oSource != null)
            {
                this.m_treeView.BeginUpdate();
                DBSourceToolsLib.Composite.IDBComposite oDBComposite = oSource.GetDBComposite();

                TreeNode parentNode = m_treeView.Nodes[oDBComposite.GetTreeNodeTag().GetTreeNodekey()];
                if (parentNode == null) // parent node is a target database
                    return;

                foreach (DBSourceToolsLib.Composite.IDBComposite oChild in oDBComposite)
                    BuildTreeView(parentNode.Nodes, oChild);
                ApplyImages(oDBComposite);
                SetExpandState();
                this.m_treeView.EndUpdate();
            }

        }

        private void BuildTreeView(TreeNodeCollection parentNodes, DBSourceToolsLib.Composite.IDBComposite oDBComposite)
        {
            if (parentNodes.ContainsKey(oDBComposite.GetTreeNodeTag().GetTreeNodekey()))
            {
                TreeNode oldNode = parentNodes[oDBComposite.GetTreeNodeTag().GetTreeNodekey()];
                parentNodes.Remove(oldNode);
                //HCSocketToolsLib.HCSocketSender.Instance().SendUDPMessage(
                //    System.Reflection.MethodBase.GetCurrentMethod()
                //    , "Removing " + oDBComposite.GetTreeNodeTag().GetTreeNodekey());

            }

            TreeNode oTreeNode = parentNodes.Add(oDBComposite.GetTreeNodeTag().GetTreeNodekey(),
                oDBComposite.GetName(), 1, 1);
            //HCSocketToolsLib.HCSocketSender.Instance().SendUDPMessage(
            //    System.Reflection.MethodBase.GetCurrentMethod()
            //    , "Adding " + oDBComposite.GetTreeNodeTag().GetTreeNodekey());

            if (oDBComposite.GetTreeNodeTag() == null)
                return;
            oTreeNode.Tag = oDBComposite.GetTreeNodeTag();
            foreach (DBSourceToolsLib.Composite.IDBComposite oChild in oDBComposite)
            {
                BuildTreeView(oTreeNode.Nodes, oChild);
            }

        }

        internal void UpdateTreeViewDatabases()
        {
            this.m_treeView.ShowNodeToolTips = true;
            this.m_treeView.BeginUpdate();
            this.m_treeView.Nodes.Clear();
            if (UserSettings.Instance().DatabaseProject != null)
            {
                foreach (SourceDatabase oDB in UserSettings.Instance().DatabaseProject.SourceDatabases)
                {
                    DBSourceToolsLib.Composite.IDBComposite oDBComposite = oDB.GetDBComposite();
                    BuildTreeView(m_treeView.Nodes, oDBComposite);
                    ApplyImages(oDBComposite);
                }
            }

            this.m_treeView.EndUpdate();
        }

        private void ApplyImages(IDBComposite oDBComposite)
        {
            SetImagesAndContext(oDBComposite, DBCompositeType.Database, 0, 0, m_contextMenuStripDatabase);
            
            SetImagesAndContext(oDBComposite, DBCompositeType.Tables, 1, 2, m_contextMenuStripTables);
            SetImagesAndContext(oDBComposite, DBCompositeType.Table, 3, 3, m_contextMenuStripTable);
            SetImagesAndContext(oDBComposite, DBCompositeType.Data, 14, 14, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.Views, 1, 2, m_contextMenuStripViews);
            SetImagesAndContext(oDBComposite, DBCompositeType.View, 4, 4, m_contextMenuStripView);
            
            SetImagesAndContext(oDBComposite, DBCompositeType.Procs, 1, 2, m_contextMenuStripProcs);
            SetImagesAndContext(oDBComposite, DBCompositeType.Proc, 5, 5, null);

            SetImagesAndContext(oDBComposite, DBCompositeType.Functions, 1, 2, m_contextMenuStripFunctions);
            SetImagesAndContext(oDBComposite, DBCompositeType.Function, 6, 6, null);

            //SetImagesAndContext(oDBComposite, DBCompositeType.DataTypes, 1, 2, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.DataType, 7, 7, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.Schema, 8, 8, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.XmlSchema, 8, 8, null);

            SetImagesAndContext(oDBComposite, DBCompositeType.Queries, 1, 2, m_contextMenuStripQueries);
            SetImagesAndContext(oDBComposite, DBCompositeType.Query, 9, 9, m_contextMenuTreeView);

            SetImagesAndContext(oDBComposite, DBCompositeType.User, 10, 10, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.Role, 11, 11, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.Table_Type, 3, 3, null);

            SetImagesAndContext(oDBComposite, DBCompositeType.Deployment_Targets, 1, 2, m_contextMenuDeployTarget);
            SetImagesAndContext(oDBComposite, DBCompositeType.Deployment_Target, 12, 12, m_contextMenuStripProperties);

            SetImagesAndContext(oDBComposite, DBCompositeType.Patches, 1, 2, m_contextMenuStripPatches);
            SetImagesAndContext(oDBComposite, DBCompositeType.Patch, 9, 9, m_contextMenuTreeView);
            SetImagesAndContext(oDBComposite, DBCompositeType.File, 9, 9, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.Field, 15, 15, null);
            SetImagesAndContext(oDBComposite, DBCompositeType.Parameter, 15, 15, null);

        }

        private void SetImagesAndContext(IDBComposite oDBComposite, 
            DBCompositeType oType, 
            int iImageIndex, 
            int iSelectedImageIndex, 
            ContextMenuStrip oContextMenuStrip)
        {
            List<string> oTables = oDBComposite.BuildTypeKeys(oType);
            foreach (string strKey in oTables)
            {
                TreeNode[] oFoundNodes = m_treeView.Nodes.Find(strKey, true);
                foreach (TreeNode oNode in oFoundNodes)
                {
                    oNode.ImageIndex = iImageIndex;
                    oNode.SelectedImageIndex = iSelectedImageIndex;
                    if (oContextMenuStrip != null)
                        oNode.ContextMenuStrip = oContextMenuStrip;
                }
            }

        }



        private void m_treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            WindowList.Instance().ShowWindow((TreeNodeTag)e.Node.Tag);
            this.Cursor = Cursors.Default;
        }


        private void newTargetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // DBExplorer | New Deployment Target
            DBHelper oHelper = new DBHelper();
            oHelper.NewDeploymentTarget(this);
            UpdateTreeViewDatabases();
        }


        private void writeTargetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // DBExplorer | Write Targets.
            try
            {
                DBHelper oHelper = new DBHelper();
                bool bRefreshData = MessageBox.Show("Refresh Data from Database ?", "Refresh Data", MessageBoxButtons.YesNo) == DialogResult.Yes;
                oHelper.SaveSourceProject(this, bRefreshData, (TreeNodeTag)m_CurrentNode.Tag);
                SetExpandState();
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(this);

            }
        }

        private void m_treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            // find current database.
            m_CurrentNode = e.Node;
            UserSettings.Instance().CurrentDbNickName = ((TreeNodeTag)e.Node.Tag).DatabaseName;
        }

        private void loadFromDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Load from database
            try
            {
                this.Cursor = Cursors.WaitCursor;
                DBHelper oHelper = new DBHelper();
                oHelper.LoadFromDatabase(this, UserSettings.Instance().CurrentDbNickName);
                this.Cursor = Cursors.Default;
                UpdateTreeView(UserSettings.Instance().CurrentDbNickName);
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(this);
            }
        }

        private void loadFromDiskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // load from disk
            try
            {
                this.Cursor = Cursors.WaitCursor;
                DBHelper oHelper = new DBHelper();
                oHelper.LoadFromDisk(this, UserSettings.Instance().CurrentDbNickName);

                this.Cursor = Cursors.Default;

                UpdateTreeView(UserSettings.Instance().CurrentDbNickName);
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(this);

//                MessageBox.Show(ex.ToString());
            }
        }

        private void newQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // context menu New Query
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQuery, this);

        }

        private void countRowsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // context menu Count Rows
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.CountDataQuery, this);
        }

        private void newTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // context menu New Table
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewTableQuery, this);
        }

        private void newProcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // context menu New Proc
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewProc, this);
        }

        private void newFunctionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // context menu New Function
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewFunction, this);
        }

        private void newQueryToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            // context Menu New Query
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQuery, this);
//            NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQuery, null);
        }

        private void countRowsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // context Count Rows
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.CountDataQuery, this);
        }

        private void newTableToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // New table
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewTableQuery, this);
        }

        private void newProcToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            // new proc
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewProc, this);
        }

        private void newFunctionToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            // new function
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewFunction, this);
        }

        #region old code moved to NewQueryHelper
        //private void NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes queryType, string strParams)
        //{
        //    try
        //    {
        //        this.Cursor = Cursors.WaitCursor;
        //        SourceDatabase oSourceDb = UserSettings.Instance().getSourceDatabase(
        //                UserSettings.Instance().CurrentDbNickName
        //            );

        //        if (oSourceDb != null)
        //        {
        //            string strNewQueryName ;//= oSourceDb.SysObjects.NewQueryName();

        //            DBSourceToolsLib.SourceDatabaseLoader oLoader = new SourceDatabaseLoader(
        //                this, queryType);

        //            oLoader.Execute(ref oSourceDb, strParams);
        //            strNewQueryName = oLoader.NewQueryFileName;

        //            UpdateTreeView(oSourceDb.NickName);

        //            WindowList.Instance().ShowWindow(new TreeNodeTag(oSourceDb.NickName, "File", strNewQueryName, null));

        //        }

        //        this.Cursor = Cursors.Default;
        //    }
        //    catch (Exception ex)
        //    {
        //        DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
        //            new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
        //        oError.ShowDialog(this);

        //        //                MessageBox.Show(ex.ToString());
        //    }

        //}
        #endregion

        private void createPatchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO : COMPLETE 1.2.0.8 creatPatch requires TargetDatabaseName
            RenameForm oNewName = new RenameForm("New Patch Name");
            oNewName.SetFileName("Patch_.sql");
            if (oNewName.ShowDialog(this) == DialogResult.OK)
            {
                NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewPatch, this, oNewName.NewName);
            }
        }

        private void compareSchemasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CompareDatabasesForm oCompForm = new CompareDatabasesForm();
                oCompForm.SourceDatabaseNick = UserSettings.Instance().CurrentDbNickName;
                if (oCompForm.ShowDialog(this) == DialogResult.OK)
                {
                    DatabaseEx oSourceDbEx = UserSettings.Instance().DatabaseProject.GetDatabaseEx(
                        oCompForm.SourceDatabaseNick);

                    SourceDatabase oSourceDb = new SourceDatabase(oSourceDbEx, false);
                    DatabaseEx oTargetDbEx = UserSettings.Instance().DatabaseProject.GetDatabaseEx(
                        oCompForm.TargetDatabaseNick);

                    if (!oSourceDbEx.CanCompare() || !oTargetDbEx.CanCompare())
                    {
                        MessageBox.Show("Schema Comparison is not supported on SQL2000 databases");
                    }
                    else
                    {

                        this.Cursor = Cursors.WaitCursor;

                        TreeNodeTag oCompareNode = new TreeNodeTag(oSourceDbEx.NickName, "CompareResults",
                            oSourceDbEx.NickName + "v" + oCompForm.TargetDatabaseNick, null);

                        WindowList.Instance().ShowWindow(oCompareNode, oCompForm.TargetDatabaseNick);

                        // TODO : COMPLETE 1.2.0.8 compare schemas notify event
                        UpdateTreeViewDatabases();
                    }
                }
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(this);

            }
            this.Cursor = Cursors.Default;

        }

        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO : COMPLETE 1.2.0.8 right click on query to rename (billhuber)
            TreeNodeTag oTag = (TreeNodeTag)m_CurrentNode.Tag;

            RenameForm oNewName = new RenameForm("Rename Query");
            oNewName.SetFileName(oTag.ObjectName);
            if (oNewName.ShowDialog(this) == DialogResult.OK)
            {
                DBSourceToolsLib.FileUtils oFileUtils = new FileUtils();
                oFileUtils.RenameFile(oTag.ObjectName, oNewName.NewName);

                // reload queries from disk.
                SourceDatabase oSourceDb = UserSettings.Instance().CurrentSourceDatabase;

                DBSourceToolsLib.SourceDatabaseLoader oSourceReloaderLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(this,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadQueriesFromDisk);
                oSourceReloaderLoader.Execute(ref oSourceDb, null);

                UpdateTreeView(oTag.DatabaseName);

            }
        }

        private void newQueryToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            // Context | Queries | New Query
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQuery, this);
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeTag oTag = (TreeNodeTag)m_CurrentNode.Tag;
            if (MessageBox.Show("Delete " + oTag.ObjectName, "Delete", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {

                if (System.IO.File.Exists(oTag.ObjectName))
                {
                    System.IO.File.Delete(oTag.ObjectName);
                }
                //delete
                SourceDatabase oSourceDb = UserSettings.Instance().CurrentSourceDatabase;
                oSourceDb.SysObjects.Delete(oTag.SysObjectKey);

                if (oTag.SysObjectKey.Contains("Patche."))
                {
                    string strTargetNick = oTag.TargetDatabaseName;
                    if (strTargetNick != null)
                    {
                        TargetDatabase oTargetDb = 
                            (TargetDatabase)UserSettings.Instance().DatabaseProject.GetDatabaseEx(strTargetNick);
                        oTargetDb.LoadPatchesFromDisk();
                    }
                }

                WindowList.Instance().RemoveWindow(oTag.DatabaseName, oTag.ObjectType, oTag.ObjectName);
            }
        }


        public void NotifyEvent(TreeNodeTag oTreeNodeTag, Interfaces.WindowListObserverEvent oEvent)
        {
            UpdateTreeView(oTreeNodeTag.DatabaseName);
            //throw new NotImplementedException();
            TreeNode [] oFoundKeys = m_treeView.Nodes.Find( oTreeNodeTag.GetTreeNodekey(), true);

            // on new query, expand the treeview.
            if (oFoundKeys.Length == 1)
            {
                ExpandToMe(oFoundKeys[0]);
                m_treeView.SelectedNode = oFoundKeys[0];
                m_treeView.Invalidate();
            }

            SetExpandState();

            switch (oEvent)
            {
                case Interfaces.WindowListObserverEvent.WindowOpened:
                    break;
                case Interfaces.WindowListObserverEvent.WindowFocused:
                    break;
                // expand the tree to show the current node
            }
        }

        private object m_LockObject = new object();
        private bool m_bIsExpanding = false;

        private void SetExpandState()
        {
            lock (m_LockObject)
                m_bIsExpanding = true;
            foreach (string strKey in m_ExpandedNodes.Keys)
            {
                TreeNode[] oFoundKeys = m_treeView.Nodes.Find(strKey, true);
                if (oFoundKeys.Length == 1)
                    ExpandToMe(oFoundKeys[0]);
            }
            lock (m_LockObject)
                m_bIsExpanding = false;
        }

        private void ExpandToMe(TreeNode treeNode)
        {
            // fixes for GUI breaking.
            //treeNode.Expand();
            //if (treeNode.Parent != null)
            //{
            //    //treeNode.Parent.Expand();
            //    ExpandToMe(treeNode.Parent);
            //}
        }

        private void m_treeView_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if (!m_bIsExpanding)
            {
                if (e.Node.Tag == null)
                    return;
                TreeNodeTag oCurrentNodeTab = (TreeNodeTag)e.Node.Tag;
                if (oCurrentNodeTab == null)
                    throw new Exception("No tag found");
                if (!m_ExpandedNodes.ContainsKey(oCurrentNodeTab.GetTreeNodekey()))
                    m_ExpandedNodes.Add(oCurrentNodeTab.GetTreeNodekey(), true);
            }

        }

        private void m_treeView_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            if (!m_bIsExpanding)
            {
                TreeNodeTag oCurrentNodeTab = (TreeNodeTag)e.Node.Tag;
                if (oCurrentNodeTab == null)
                    throw new Exception("No tag found");
                if (m_ExpandedNodes.ContainsKey(oCurrentNodeTab.GetTreeNodekey()))
                    m_ExpandedNodes.Remove(oCurrentNodeTab.GetTreeNodekey());
            }

        }

        private void scriptInsertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeTag oCurrentNodeTag = (TreeNodeTag)m_CurrentNode.Tag;
            NewQueryHelper.NewQuery(
                DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQueryScriptInsert, this, oCurrentNodeTag.ObjectName);

        }

        private void scriptSelectFromToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeTag oCurrentNodeTag = (TreeNodeTag)m_CurrentNode.Tag;
            NewQueryHelper.NewQuery(
                DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQueryScriptSelect , this, oCurrentNodeTag.ObjectName);

        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            WindowList.Instance().ShowWindow((TreeNodeTag)m_CurrentNode.Tag);
            this.Cursor = Cursors.Default;
        }

        private void propertiesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            WindowList.Instance().ShowWindow((TreeNodeTag)m_CurrentNode.Tag);
            this.Cursor = Cursors.Default;
        }

        private void newTableToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            // Table | New Table
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewTableQuery, this);
        }

        private void newStoredProcedureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Procs | New Stored Procedure
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewProc, this);
        }

        private void newFunctionToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewFunction, this);
        }

        private void newViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewView, this);
        }

        private void scriptSelectFromToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TreeNodeTag oCurrentNodeTag = (TreeNodeTag)m_CurrentNode.Tag;
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQuerySelectView, this, oCurrentNodeTag.ObjectName);
        }

        private void scriptUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeTag oCurrentNodeTag = (TreeNodeTag)m_CurrentNode.Tag;
            NewQueryHelper.NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQueryUpdateTable, this, oCurrentNodeTag.ObjectName);
        }

        private void newPatchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeTag oCurrentNodeTag = (TreeNodeTag)m_CurrentNode.Tag;
            NewQueryHelper.NewPatch(this, oCurrentNodeTag);
        }
    }
}
